package java44th.group.service.admin.topic;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java44th.group.bean.admin.bo.topic.TopicBatchDeleteByIdBo;
import java44th.group.bean.admin.bo.topic.TopicCreateBo;
import java44th.group.bean.admin.bo.topic.TopicListBo;
import java44th.group.bean.admin.bo.topic.TopicReadById;
import java44th.group.bean.common.CommonData;
import java44th.group.bean.po.MarketGoods;
import java44th.group.bean.po.MarketGoodsExample;
import java44th.group.bean.po.MarketTopic;
import java44th.group.bean.po.MarketTopicExample;
import java44th.group.mapper.MarketGoodsMapper;
import java44th.group.mapper.MarketTopicMapper;
import java44th.group.utils.LocalHost;
import java44th.group.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Aurhor: 帅的一批
 * @CreateAt: 2022/10/13 11:19
 * @Description:
 * @PACKAGE_NAME： java44th.group.service.admin.topic
 */

@Service
public class TopicServiceImpl implements TopicService {

    @Autowired
    MarketTopicMapper marketTopicMapper;

    @Autowired
    MarketGoodsMapper goodsMapper;

    @Override
    public CommonData getTopicList(TopicListBo topicListBo) {
        List<MarketTopic> marketTopics = null;
        try {
            MarketTopicExample marketTopicExample = new MarketTopicExample();
            marketTopicExample.setDistinct(false);
            marketTopicExample.setOrderByClause(topicListBo.getSort() + " " + topicListBo.getOrder());

            MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
            if (!StringUtil.isEmpty(topicListBo.getSubtitle())) {
                criteria.andSubtitleLike("%" + topicListBo.getSubtitle() + "%");
            }
            if (!StringUtil.isEmpty(topicListBo.getTitle())) {
                criteria.andTitleLike("%" + topicListBo.getTitle() + "%");
            }
            //查询为删除的
            criteria.andDeletedEqualTo(false);
            PageHelper.startPage(topicListBo.getPage(), topicListBo.getLimit());
            marketTopics = marketTopicMapper.selectByExample(marketTopicExample);

            //拼接图片url
            for (MarketTopic marketTopic : marketTopics) {
                if (!marketTopic.getPicUrl().startsWith("http")) {
                    marketTopic.setPicUrl(LocalHost.localhost + marketTopic.getPicUrl());
                }
            }

            PageInfo<MarketTopic> marketTopicPageInfo = new PageInfo<>(marketTopics);
            return CommonData.data(marketTopicPageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public MarketTopic createTopic(TopicCreateBo topicCreateBo) {
        try {
            MarketTopic marketTopic = new MarketTopic(
                    null,
                    topicCreateBo.getTitle(),
                    topicCreateBo.getSubtitle(),
                    new BigDecimal(topicCreateBo.getDoublePrice()),
                    topicCreateBo.getReadCount(),
                    topicCreateBo.getPicUrl().replaceAll(LocalHost.localhost, ""),
                    null,
                    topicCreateBo.getGoods(),
                    new Date(),
                    new Date(),
                    false,
                    topicCreateBo.getContent()
            );
            marketTopicMapper.insertSelective(marketTopic);
            return marketTopic;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public TopicReadById getTopicByid(Integer id) {
        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);
        if (!marketTopic.getPicUrl().startsWith("http")){
            marketTopic.setPicUrl(LocalHost.localhost + marketTopic.getPicUrl());
        }
        List<Integer> marketGoods = new ArrayList<>();
        if (marketTopic.getGoods()!=null && marketTopic.getGoods().length==0){

        }
        //字符数组转为list
        for (Integer goodId : marketTopic.getGoods()) {
            marketGoods.add(goodId);
        }
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        List<MarketGoods> marketGoods1 = null;
        //如果有goods 就查询 没有就直接过
        if (marketGoods.size()>=1){
            criteria.andIdIn(marketGoods);
            marketGoods1 = goodsMapper.selectByExample(marketGoodsExample);
            for (MarketGoods goods : marketGoods1) {
                if (!goods.getPicUrl().startsWith("http")){
                    goods.setPicUrl(LocalHost.localhost + goods.getPicUrl());
                }
            }
        }
        //拼接图片url

        return new TopicReadById(
                marketGoods1,
                marketTopic
        );
    }

    @Override
    public void virtualDelete(MarketTopic marketTopic) {
        MarketTopic marketTopic1 = new MarketTopic();
        marketTopic1.setId(marketTopic.getId());
        marketTopic1.setUpdateTime(marketTopic.getUpdateTime());
        marketTopic1.setSortOrder(marketTopic.getSortOrder());
        marketTopic1.setDeleted(true);
        marketTopicMapper.updateByPrimaryKeySelective(marketTopic1);
    }

    @Override
    public void virtualDeleteBatchTopicByid(TopicBatchDeleteByIdBo topicBatchDeleteByIdBo) {
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        criteria.andIdIn(topicBatchDeleteByIdBo.getIds());

        MarketTopic marketTopic = new MarketTopic();
        marketTopic.setDeleted(true);
        marketTopic.setUpdateTime(new Date());

        marketTopicMapper.updateByExampleSelective(marketTopic, marketTopicExample);

    }

    @Override
    public MarketTopic updateTopic(MarketTopic marketTopic) {
        marketTopic.setUpdateTime(new Date());
        marketTopicMapper.updateByPrimaryKeySelective(marketTopic);
        return marketTopic;
    }
}
